package com.powerManager.sdk.handlers;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.jdbc.core.JdbcTemplate;

import com.powerManager.sdk.common.JdbcSingleton;
import com.powerManager.sdk.common.PagingResult;

public class ObjectColumnHandler {

	public String loadJSON(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sql="select col_id,column_name columnName,show_name showName,remark from sys_obj_column where obj_id=?";
		String page = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		PagingResult pr = new PagingResult(sql);
		String json = null;
		String id = request.getParameter("ID");
		try {
			
			json = pr.pagingToJson(new Object[]{id},Integer.parseInt(pageSize), Integer.parseInt(page));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(json);
		return "text:"+json;
	}
	
	public String removeColumn(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sql = "delete from sys_obj_column where col_id=?";
		JdbcTemplate jdbc = JdbcSingleton.getJdbcTemplate();
		Object args = request.getParameter("ID");
		jdbc.update(sql, args);
		return "text:true";
	}
	public String updateColumn(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sql = "update sys_obj_column set column_name=?,show_name=?,remark=? where col_id=?";
		JdbcTemplate jdbc = JdbcSingleton.getJdbcTemplate();
		Object[] args = new Object[4];
		args[0] = request.getParameter("columnName");
		args[1] = request.getParameter("showName");
		args[2] = request.getParameter("remark");
		args[3] = request.getParameter("col_id");
		jdbc.update(sql, args);
		return "text:true";
	}
	public String addColumn(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sql = "insert into sys_obj_column(col_id,column_name,show_name,remark,obj_id) values(GET_SEQ('SEQ_COLUMN_ID','0'),?,?,?,?)";
		JdbcTemplate jdbc = JdbcSingleton.getJdbcTemplate();
		Object[] args = new Object[4];
		args[0] = request.getParameter("columnName");
		args[1] = request.getParameter("showName");
		args[2] = request.getParameter("remark");
		args[3] = request.getParameter("obj_id");
		jdbc.update(sql, args);
		return "text:true";
	}
}
